﻿using NHibernate.Mapping.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.DataStore.Models
{

    /// <summary>
    /// 数据库模型基类
    /// </summary>
    public class ModelBase
    {
        public ModelBase()
        {
            //默认没有赋值的时候，时间为最小值
            if (CreateTime == DateTime.MinValue)
            {
                CreateTime = DateTime.Now;
            }
            UpdateTime = DateTime.Now;
        }

        /// <summary>
        /// 可自动生成id的构造函数
        /// </summary>
        /// <param name="genIdAuto">自动生成guid</param>
        public ModelBase(bool genIdAuto=true):base()
        {
            if (genIdAuto)
            {
                Id = Guid.NewGuid().ToString("N");
            }
            //默认没有赋值的时候，时间为最小值
            if (CreateTime == DateTime.MinValue)
            {
                CreateTime = DateTime.Now;
            }
            UpdateTime = DateTime.Now;
        }

        /// <summary>
        /// 主键 Id,需要手动赋值
        /// </summary>
        /// <remarks>采用字符串，通用,不过id 需要自己分配</remarks>
        [Id(0, Name = "Id", TypeType = typeof(string), Length = 255)]
        [Key(1, Unique = true)]
        [Generator(2, Class = "assigned")]
        public virtual string Id { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [Property(Column = "CreateTime", TypeType = typeof(DateTime))]
        public virtual DateTime CreateTime { get; set; } = DateTime.MinValue;
        /// <summary>
        /// 更新时间
        /// </summary>
        [Property(Column = "UpdateTime", TypeType = typeof(DateTime))]
        public virtual DateTime UpdateTime { get; set; } = DateTime.MinValue;
        /// <summary>
        /// 创建人id
        /// </summary>
        [Property(Column = "CreatorId", TypeType = typeof(string), Length = 255)]
        public virtual string CreatorId { get; set; } = string.Empty;
        /// <summary>
        /// 更新人id
        /// </summary>
        [Property(Column = "UpdaterId", TypeType = typeof(string), Length = 255)]
        public virtual string UpdaterId { get; set; } = string.Empty;
    }
}
